<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="recommendAudioList" style="height: 100%; width: 100%;">

</table>

<div id="recommendAudioListToolBars" style="height: auto">

	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="recommendAudioList_remove()">删除</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-add',plain:true"
		onclick="recommendAudioList_append()">添加</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="recommendAudioList_edit()">编辑</a> | <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="recommendAudioList_undo()">取消</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="recommendAudioList_accept()">保存</a> |<input
		id="recommendAudioListPlatformQue" style="width: 100px;" /> |<input
		id="recommendAudioList-AudioStatusQue" style="width: 100px;" /> |<input
		id="recommendAudioList-AudioProgressStatusQue" style="width: 100px;" />
	|<input id="recommendAudioListTitleQue" style="width: 200px;" />
</div>
<script type="text/javascript">
	function recommendAudioListDatagridReload() {
		try {
			var queryParams = {
				like_title : recommendAudioListTitleQue.textbox('getValue'),
				itemType : 2,
				platform : recommendAudioListPlatformQue.combobox('getValue'),
				status : recommendAudioList_AudioStatusQue.combobox('getValue'),
				progressStatus : recommendAudioList_AudioProgressStatusQue
						.combobox('getValue')
			};
			recommendAudioListDatagrid.datagrid("options").queryParams = queryParams;
			recommendAudioListDatagrid.datagrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var recommendAudioList_AudioProgressStatusQue = undefined;
	var recommendAudioList_AudioStatusQue = undefined;
	var recommendAudioListPlatformQue = undefined;
	var recommendAudioListTitleQue = undefined;
	var recommendAudioSelectIndex = undefined;
	var recommendAudioSelectRow = undefined;
	var recommendAudioEditIndex = undefined;
	var recommendAudioEditRow = undefined;
	var recommendAudioListDatagrid = undefined;
	$(function() {

		recommendAudioList_AudioProgressStatusQue = $(
				'#recommendAudioList-AudioProgressStatusQue').combobox({
			data : [ {
				"progressStatusCode" : 0,
				"progressStatusMsg" : "全部音频进度",
				"selected" : true
			}, {
				"progressStatusCode" : 1,
				"progressStatusMsg" : "连载中"
			}, {
				"progressStatusCode" : 2,
				"progressStatusMsg" : "已完结"
			} ],
			valueField : 'progressStatusCode',
			textField : 'progressStatusMsg',
			onChange : function(newValue, oldValue) {
				recommendAudioListDatagridReload();
			}
		});

		recommendAudioList_AudioStatusQue = $(
				'#recommendAudioList-AudioStatusQue').combobox({
			data : [ {
				"statusCode" : 0,
				"statusMsg" : "全部音频状态",
				"selected" : true
			}, {
				"statusCode" : 1,
				"statusMsg" : "已发布"
			}, {
				"statusCode" : 2,
				"statusMsg" : "已下线"
			}, {
				"statusCode" : 3,
				"statusMsg" : "待发布"
			} ],
			valueField : 'statusCode',
			textField : 'statusMsg',
			onChange : function(newValue, oldValue) {
				recommendAudioListDatagridReload();
			}
		});

		recommendAudioListPlatformQue = $('#recommendAudioListPlatformQue')
				.combobox({
					data : [ {
						"platformCode" : 0,
						"platformMsg" : "不分平台",
						"selected" : true
					}, {
						"platformCode" : 1,
						"platformMsg" : "所有平台"
					}, {
						"platformCode" : 2,
						"platformMsg" : "web和wap"
					}, {
						"platformCode" : 3,
						"platformMsg" : "android和ios"
					}, {
						"platformCode" : 4,
						"platformMsg" : "web"
					}, {
						"platformCode" : 5,
						"platformMsg" : "wap"
					}, {
						"platformCode" : 6,
						"platformMsg" : "android"
					}, {
						"platformCode" : 7,
						"platformMsg" : "ios"
					} ],
					valueField : 'platformCode',
					textField : 'platformMsg',
					onChange : function(newValue, oldValue) {
						recommendAudioListDatagridReload();
					}
				});

		recommendAudioListTitleQue = $('#recommendAudioListTitleQue').textbox({
			buttonText : '音频标题搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		recommendAudioListTitleQue.textbox('button').click(function() {
			recommendAudioListDatagridReload();
		});

		recommendAudioListDatagrid = $('#recommendAudioList')
				.datagrid(
						{
							url : '${baseUrl}/recommend/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								itemType : 2
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#recommendAudioListToolBars',
							onLoadSuccess : function() {
								recommendAudioSelectIndex = undefined;
								recommendAudioSelectRow = undefined;
								recommendAudioEditIndex = undefined;
								recommendAudioEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (recommendAudioEditIndex != undefined) {
									return false;
								} else {
									recommendAudioSelectIndex = index;
									recommendAudioSelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (recommendAudioEditIndex == undefined) {
									recommendAudioEditIndex = index;
									recommendAudioEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
								var _ed = $(this).datagrid('getEditor', {
									index : index,
									field : 'platformCode'
								});
								row.platformMsg = $(_ed.target).combobox(
										'getText');
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.coverUrl) {
												var img = '<img src="'
														+ row.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'info',
										title : '推荐信息',
										width : 300,
										editor : {
											type : 'text',
											options : {
												required : true
											}
										}
									},
									{
										field : 'platformCode',
										title : '推荐平台',
										width : 120,
										align : 'center',
										formatter : function(value, row, index) {
											return row.platformMsg;
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'platformCode',
												textField : 'platformMsg',
												data : [
														{
															"platformCode" : 1,
															"platformMsg" : "所有平台"
														},
														{
															"platformCode" : 2,
															"platformMsg" : "web和wap"
														},
														{
															"platformCode" : 3,
															"platformMsg" : "android和ios"
														},
														{
															"platformCode" : 4,
															"platformMsg" : "web"
														},
														{
															"platformCode" : 5,
															"platformMsg" : "wap"
														},
														{
															"platformCode" : 6,
															"platformMsg" : "android"
														},
														{
															"platformCode" : 7,
															"platformMsg" : "ios"
														} ],
												required : true
											}
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 60,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'startTime',
										title : '开始时间',
										width : 100,
										align : 'left',
										editor : {
											type : 'datetimebox',
											options : {
												required : true
											}
										}
									},
									{
										field : 'endTime',
										title : '结束时间',
										width : 100,
										align : 'left',
										editor : {
											type : 'datetimebox',
											options : {
												required : true
											}
										}
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemId',
										title : '音频ID',
										width : 80,
										align : 'center'
									},
									{
										field : 'title',
										title : '音频标题',
										width : 249
									},
									{
										field : 'entity',
										title : '音频实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'itemParent',
										title : '所属音频集合',
										width : 249,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.itemParent) {
												return "<span>集合ID:"
														+ row.itemParent.id
														+ "</span><br/><span>集合标题:"
														+ row.itemParent.title
														+ "</span>";
											} else {
												return value;
											}
										}
									},
									{
										field : 'playtime',
										title : '播放时间(秒)',
										width : 100,
										align : 'center'
									},
									{
										field : 'statusCode',
										title : '音频状态',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										}
									},
									{
										field : 'progressStatusCode',
										title : '音频进度',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										}
									} ] ],
							onDblClickRow : function(index, row) {
								$.ajax({
									data : {
										id : row.itemId
									},
									async : false,
									url : baseUrl + '/audio/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		recommendAudioListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							recommendAudioListDatagrid.datagrid('gotoPage',
									recommendAudioListDatagrid.datagrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});

	});

	function recommendAudioList_append() {
		$.ajax({
			url : baseUrl + '/recommend/add.html?itemType=2',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function recommendAudioList_edit() {

		var recommendAudioSelect = recommendAudioListDatagrid
				.datagrid('getSelected');
		if (recommendAudioEditIndex != undefined
				&& recommendAudioEditIndex != recommendAudioSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (recommendAudioSelect) {
			if (recommendAudioSelectIndex == undefined) {
				recommendAudioSelectIndex = recommendAudioListDatagrid
						.datagrid("getRowIndex", recommendAudioSelect);
			}

			recommendAudioListDatagrid.datagrid('beginEdit',
					recommendAudioSelectIndex);
			recommendAudioSelectRow = recommendAudioSelect;
		}
	}

	function recommendAudioList_undo() {
		if (recommendAudioSelectIndex != undefined) {
			recommendAudioListDatagrid.datagrid('rejectChanges');
			recommendAudioSelectIndex = undefined;
			recommendAudioSelectRow = undefined;
			recommendAudioEditIndex = undefined;
			recommendAudioEditRow = undefined;
		}
	}

	function recommendAudioList_accept() {
		if (recommendAudioSelectIndex != undefined) {
			if (recommendAudioListDatagrid.datagrid('validateRow',
					recommendAudioSelectIndex)) {

				//验证推荐平台
				var _edPlatformCode = recommendAudioListDatagrid.datagrid(
						'getEditor', {
							index : recommendAudioSelectIndex,
							field : 'platformCode'
						});
				var _PlatformMsg = $(_edPlatformCode.target)
						.combobox('getText');

				if (_PlatformMsg != '所有平台' && _PlatformMsg != 'web和wap'
						&& _PlatformMsg != 'android和ios'
						&& _PlatformMsg != 'web' && _PlatformMsg != 'wap'
						&& _PlatformMsg != 'android' && _PlatformMsg != 'ios') {
					$.messager.alert('提示', '未知平台', 'warning');
					return;
				}

				//验证推荐信息
				var _edInfo = recommendAudioListDatagrid.datagrid('getEditor',
						{
							index : recommendAudioSelectIndex,
							field : 'info'
						});

				var _Info = $(_edInfo.target).val();

				//获取排序
				var _edSort = recommendAudioListDatagrid.datagrid('getEditor',
						{
							index : recommendAudioSelectIndex,
							field : 'sort'
						});

				var _Sort = $(_edSort.target).val();

				//验证开始时间
				var _edstartTime = recommendAudioListDatagrid.datagrid(
						'getEditor', {
							index : recommendAudioSelectIndex,
							field : 'startTime'
						});
				var _startTime = $(_edstartTime.target).datetimebox('getValue');

				if (!_startTime) {
					$.messager.alert('提示', '开始时间不能为空', 'warning');
					return;
				}

				//验证结束时间
				var _edendTime = recommendAudioListDatagrid.datagrid(
						'getEditor', {
							index : recommendAudioSelectIndex,
							field : 'endTime'
						});
				var _endTime = $(_edendTime.target).datetimebox('getValue');
				if (!_endTime) {
					$.messager.alert('提示', '结束时间不能为空', 'warning');
					return;
				}

				var _Data = {
					id : recommendAudioSelectRow.id,
					info : _Info,
					sort : _Sort,
					platform : $(_edPlatformCode.target).combobox('getValue'),
					startTimeStr : _startTime,
					endTimeStr : _endTime
				};

				$.ajax({
					data : _Data,
					async : false,
					type : 'POST',
					url : baseUrl + '/recommend/update',
					success : function(data) {
						if (data.responseCode == '000') {
							recommendAudioListDatagrid.datagrid('endEdit',
									recommendAudioSelectIndex);

							recommendAudioListDatagrid
									.datagrid('acceptChanges');

							recommendAudioSelectIndex = undefined;
							recommendAudioSelectRow = undefined;
							recommendAudioEditIndex = undefined;
							recommendAudioEditRow = undefined;

							$.messager.alert('提示', '修改成功', 'info');
						} else {
							$.messager.alert('提示', data.responseMessage,
									'error');
						}
					}
				});

			}
		}
	}

	function recommendAudioList_remove() {
		var recommendAudioSelect = recommendAudioListDatagrid
				.datagrid('getSelected');
		if (recommendAudioSelect) {
			$.messager.confirm('确认', '您确认要删除么吗？', function(r) {
				if (r) {
					$.ajax({
						data : {
							ids : [ recommendAudioSelect.id ]
						},
						type : "post",
						async : false,
						url : baseUrl + '/recommend/delete',
						success : function(data) {
							if (data.responseCode == '000') {
								$.messager.alert('提示', "删除成功!", 'info');

								recommendAudioListDatagrid.datagrid(
										'deleteRow', recommendAudioListDatagrid
												.datagrid('getRowIndex',
														recommendAudioSelect));

							} else {
								$.messager.alert('提示', data.responseMessage,
										'error');
							}
						}
					});
				}
			});
		}
	}
</script>
